package com.seleniti.enpareja.android.ui.view;

import org.ioc.commons.impl.android.common.util.ListViewPagedDataProviderAdapter;
import org.ioc.commons.impl.android.ui.HasEnabledWrapper;
import org.ioc.commons.impl.android.ui.HasValueWrapper;
import org.ioc.commons.impl.android.ui.HasVisibilityWrapper;
import org.ioc.commons.model.dataprovider.PagedDataProvider;
import org.ioc.commons.ui.HasValue;
import org.ioc.commons.ui.HasVisibility;
import org.ioc.commons.ui.features.Info;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;

import com.seleniti.enpareja.android.R;
import com.seleniti.enpareja.android.ui.widget.InfoInlineWidget;
import com.seleniti.enpareja.controller.model.integration.database.domain.CoupleType;
import com.seleniti.enpareja.controller.model.integration.database.domain.User;
import com.seleniti.enpareja.controller.ui.view.CoupleRequestsView;
import com.seleniti.enpareja.controller.ui.view.CoupleRequestsView.Action;
import com.seleniti.enpareja.controller.ui.view.CoupleRequestsView.Event;
import com.seleniti.enpareja.controller.ui.view.CoupleRequestsView.Operation;
import com.seleniti.enpareja.controller.ui.view.CoupleRequestsView.Presenter;
import com.seleniti.enpareja.controller.ui.view.CoupleRequestsViewPresenter;

public class CoupleRequestsViewWidget extends EnParejaBaseView<Event, Operation, Action, Presenter> implements
		CoupleRequestsView, OnQueryTextListener {

	private SearchView searchBox;
	private ListView lstUsers;
	private InfoInlineWidget ableToGetCoupleInfo;
	private InfoInlineWidget unableToGetCoupleInfo;
	private Button btnMakeCoupleRequest;
	private InfoInlineWidget requestSentInfo;
	private InfoInlineWidget requestAlreadyMadeInfo;
	private InfoInlineWidget coupleGotInfo;
	private boolean loading;
	private ListViewPagedDataProviderAdapter<User, Operation> pagedDataProvider;
	private int selected = -1;

	public CoupleRequestsViewWidget() {
		super(false);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setRetainInstance(true);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		this.presenter = new CoupleRequestsViewPresenter(this, eventbus, operationManager, actionController);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.couple_request_view, container, false);

		this.searchBox = (SearchView) view.findViewById(R.id.searchBox);
		this.lstUsers = (ListView) view.findViewById(R.id.lstUsers);
		this.ableToGetCoupleInfo = (InfoInlineWidget) view.findViewById(R.id.ableToGetCoupleInfo);
		this.unableToGetCoupleInfo = (InfoInlineWidget) view.findViewById(R.id.unableToGetCoupleInfo);
		this.btnMakeCoupleRequest = (Button) view.findViewById(R.id.btnMakeCoupleRequest);
		this.requestSentInfo = (InfoInlineWidget) view.findViewById(R.id.requestSentInfo);
		this.requestAlreadyMadeInfo = (InfoInlineWidget) view.findViewById(R.id.requestAlreadyMadeInfo);
		this.coupleGotInfo = (InfoInlineWidget) view.findViewById(R.id.coupleGotInfo);

		TextView noResults = (TextView) view.findViewById(R.id.noResults);

		this.lstUsers.setTextFilterEnabled(true);
		this.lstUsers.setEmptyView(noResults);
		this.lstUsers.setSelector(R.drawable.list_selector);
		this.lstUsers.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				boolean notify = CoupleRequestsViewWidget.this.selected != position;
				CoupleRequestsViewWidget.this.selected = position;

				if (notify && pagedDataProvider != null) {
					pagedDataProvider.notifyDataSetChanged();
					presenter.onSelectedUserChanged();
				}
			}
		});

		this.btnMakeCoupleRequest.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				presenter.onMakeCoupleRequest();
			}
		});

		this.searchBox.setOnQueryTextListener(this);
		this.searchBox.setIconifiedByDefault(false);

		return view;
	}

	@Override
	public boolean onQueryTextChange(String newText) {
		startSearch();
		return true;
	}

	@Override
	public boolean onQueryTextSubmit(String query) {
		return false;
	}

	private static class ItemViewHolder {

		protected TextView email;
		protected TextView name;
		protected TextView lblMatchedWith;
		protected TextView matchedWith;

	}

	@Override
	public void setPagedDataProvider(PagedDataProvider<User> dataProvider) {
		int resource = 0;
		int textResource = 0;
		this.pagedDataProvider = new ListViewPagedDataProviderAdapter<User, Operation>(this.lstUsers, dataProvider,
				resource, textResource, Operation.GettingUsers, operationManager, this) {
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				ItemViewHolder ivh = null;
				if (convertView == null) {

					Bundle savedInstanceState = new Bundle();
					LayoutInflater inflater = getLayoutInflater(savedInstanceState);
					convertView = inflater.inflate(R.layout.user_layout, parent, false);

					ivh = new ItemViewHolder();

					ivh.email = (TextView) convertView.findViewById(R.id.email);
					ivh.name = (TextView) convertView.findViewById(R.id.name);
					ivh.lblMatchedWith = (TextView) convertView.findViewById(R.id.lblMatchedWidth);
					ivh.matchedWith = (TextView) convertView.findViewById(R.id.matchedWith);

					convertView.setTag(ivh);
				} else {
					ivh = (ItemViewHolder) convertView.getTag();
				}

				User user = this.getItem(position);
				ivh.email.setText(maskEmail(user.getUserEmail()));
				ivh.name.setText(user.getNickName());

				if (user.getGender() != null) {
					switch (user.getGender()) {
					case FEMALE:
						ivh.name.setCompoundDrawablesWithIntrinsicBounds(R.drawable.female, 0, 0, 0);
						break;
					case MALE:
						ivh.name.setCompoundDrawablesWithIntrinsicBounds(R.drawable.male, 0, 0, 0);
						break;
					}
				} else {
					ivh.name.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
				}

				if (user.getCouple() != null && !user.getCouple().getUserEmail().isEmpty()) {
					if (!user.getCouple().isFakeUser()) {
						ivh.lblMatchedWith.setVisibility(View.VISIBLE);
						ivh.lblMatchedWith.setText(getString(R.string.matched_with));
						ivh.matchedWith.setText(user.getCouple().getNickName());
					} else if (user.getCoupleType() == CoupleType.SINGLE_USER) {
						ivh.lblMatchedWith.setVisibility(View.VISIBLE);
						ivh.lblMatchedWith.setText(getString(R.string.single));
						ivh.matchedWith.setText("");
					} else {
						ivh.lblMatchedWith.setVisibility(View.GONE);
						ivh.matchedWith.setText("");
					}
				} else if (user.getRequestsToYou() != null && !user.getRequestsToYou().isEmpty()) {
					ivh.lblMatchedWith.setVisibility(View.VISIBLE);
					ivh.matchedWith.setText("Pendiente de confirmar pareja");
				} else {
					ivh.lblMatchedWith.setVisibility(View.GONE);
					ivh.matchedWith.setText("");
				}

				if (selected != -1 && selected == position) {
					convertView.setSelected(true);
					convertView.setBackgroundColor(Color.parseColor("#628CD5"));
				} else {
					convertView.setSelected(false);
					convertView.setBackgroundColor(Color.WHITE);
				}

				return convertView;
			}
		};
	}

	@Override
	public PagedDataProvider<User> getPagedDataProvider() {
		return this.pagedDataProvider;
	}

	@Override
	public void setLoading(boolean loading) {
		this.loading = loading;
		HasEnabledWrapper.setEnabled(!loading, btnMakeCoupleRequest, searchBox);
	}

	@Override
	public boolean isLoading() {
		return this.loading;
	}

	@Override
	public void startSearch() {
		// if (this.queryText == null || this.queryText.isEmpty()) {
		// this.lstUsers.clearTextFilter();
		// } else {
		// this.lstUsers.setFilterText(this.queryText);
		// }
		pagedDataProvider.reload();
	}

	@Override
	public HasValue<String> searchBox() {
		return HasValueWrapper.from(this.searchBox, true, this);
	}

	@Override
	public User getSelectedUser() {
		// User selectedItem = (User) this.lstUsers.getSelectedItem();
		User selectedItem = selected < 0 || selected > (this.lstUsers.getCount() - 1) ? null : (User) this.lstUsers
				.getItemAtPosition(selected);

		return selectedItem;
	}

	@Override
	public HasVisibility ableToGetCoupleInfo() {
		return HasVisibilityWrapper.from(this.ableToGetCoupleInfo, this);
	}

	@Override
	public HasVisibility unableToGetCoupleInfo() {
		return HasVisibilityWrapper.from(this.unableToGetCoupleInfo, this);
	}

	@Override
	public HasVisibility makeCoupleRequestActionVisibility() {
		return HasVisibilityWrapper.from(this.btnMakeCoupleRequest, this);
	}

	@Override
	public HasVisibility requestSentInfo() {
		return HasVisibilityWrapper.from(this.requestSentInfo, this);
	}

	@Override
	public HasVisibility requestAlreadyMadeInfo() {
		return HasVisibilityWrapper.from(this.requestAlreadyMadeInfo, this);
	}

	@Override
	public Info coupleGotInfo() {
		return this.coupleGotInfo;
	}

	@Override
	public void clearSelection() {
		this.lstUsers.clearChoices();

	}

}
